﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AutoduelClient.GameUI
{
    public delegate void ButtonClickEventHandler(object sender, ButtonClickEventArgs e);
    
    /// <summary>
    /// Remake of the standard .NET MessageBox class
    /// to allow
    /// </summary>
    public partial class AsyncMessageBox : Form
    {

        public event ButtonClickEventHandler ButtonClicked;

        public AsyncMessageBox()
        {
            InitializeComponent();
        }

        public AsyncMessageBox(string message, string caption, MessageBoxButtons messageBoxButtons, MessageBoxIcon messageBoxIcon)
        {
            InitializeComponent();


            SetUpMessageBox(message, caption, messageBoxButtons, messageBoxIcon);
        }

        public AsyncMessageBox(ButtonClickEventHandler eventHandler, string message, string caption, MessageBoxButtons messageBoxButtons, MessageBoxIcon messageBoxIcon)
        {
            InitializeComponent();

            SetUpMessageBox(message, caption, messageBoxButtons, messageBoxIcon);

            this.ButtonClicked += new ButtonClickEventHandler(eventHandler);
        }

        private void SetUpMessageBox(string message, string caption, MessageBoxButtons messageBoxButtons, MessageBoxIcon messageBoxIcon)
        {
            this.messageText.Text = message;
            this.Text = caption;

            switch (messageBoxButtons)
            {
                case MessageBoxButtons.AbortRetryIgnore:
                    {
                        this.CancelButton = button3;
                        button3.Text = "Abort";
                        button3.Tag = DialogResult.Abort;
                        this.AcceptButton = button2;
                        button2.Text = "Retry";
                        button2.Tag = DialogResult.Retry;
                        button1.Text = "Ignore";
                        button1.Tag = DialogResult.Ignore;
                        break;
                    }
                case MessageBoxButtons.OK:
                    {
                        button3.Visible = false;
                        button2.Visible = false;
                        this.AcceptButton = button1;
                        button1.Text = "OK";
                        button1.Tag = DialogResult.OK;
                        break;
                    }
                case MessageBoxButtons.OKCancel:
                    {
                        button3.Visible = false;
                        this.AcceptButton = button2;
                        button2.Text = "OK";
                        button2.Tag = DialogResult.OK;
                        this.CancelButton = button1;
                        button1.Text = "Cancel";
                        button1.Tag = DialogResult.Cancel;
                        break;
                    }
                case MessageBoxButtons.RetryCancel:
                    {
                        button3.Visible = false;
                        this.AcceptButton = button2;
                        button2.Text = "Retry";
                        button2.Tag = DialogResult.Retry;
                        this.CancelButton = button1;
                        button1.Text = "Cancel";
                        button1.Tag = DialogResult.Cancel;
                        break;
                    }
                case MessageBoxButtons.YesNo:
                    {
                        button3.Visible = false;
                        this.AcceptButton = button2;
                        button2.Text = "Yes";
                        button2.Tag = DialogResult.Yes;
                        this.CancelButton = button1;
                        button1.Text = "No";
                        button1.Tag = DialogResult.No;
                        break;
                    }
                case MessageBoxButtons.YesNoCancel:
                    {
                        this.AcceptButton = button3;
                        button3.Text = "Yes";
                        button3.Tag = DialogResult.Yes;
                        button2.Text = "No";
                        button2.Tag = DialogResult.No;
                        this.CancelButton = button1;
                        button1.Text = "Cancel";
                        button1.Tag = DialogResult.Cancel;
                        break;
                    }
            }

            switch (messageBoxIcon)
            {
                case MessageBoxIcon.Asterisk:
                    {
                        this.pictureBox1.Image = Bitmap.FromHicon(SystemIcons.Asterisk.Handle);
                        break;
                    }
                case MessageBoxIcon.Error:
                    {
                        this.pictureBox1.Image = Bitmap.FromHicon(SystemIcons.Error.Handle);
                        break;
                    }
                case MessageBoxIcon.Exclamation:
                    {
                        this.pictureBox1.Image = Bitmap.FromHicon(SystemIcons.Exclamation.Handle);
                        break;
                    }
                case MessageBoxIcon.Question:
                    {
                        this.pictureBox1.Image = Bitmap.FromHicon(SystemIcons.Question.Handle);
                        break;
                    }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Owner.Enabled = true;
            if (ButtonClicked != null)
            {
                ButtonClicked(sender, new ButtonClickEventArgs((DialogResult)button1.Tag));
            }
            
            this.DialogResult = (DialogResult)button1.Tag;
            this.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Owner.Enabled = true;

            if (ButtonClicked != null)
            {
                ButtonClicked(sender, new ButtonClickEventArgs((DialogResult)button2.Tag));
            }

            this.DialogResult = (DialogResult)button2.Tag;
            this.Dispose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Owner.Enabled = true;

            if (ButtonClicked != null)
            {
                ButtonClicked(sender, new ButtonClickEventArgs((DialogResult)button3.Tag));
            }

            this.DialogResult = (DialogResult)button3.Tag;
            this.Dispose();
        }

        private void AsyncMessageBox_Load(object sender, EventArgs e)
        {
            this.Location = new Point(this.Owner.Location.X + this.Owner.Width / 2 - this.Width / 2, this.Owner.Location.Y + this.Owner.Height / 2 - this.Height / 2);
        }

        private void AsyncMessageBox_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Owner.Enabled = true;
        }
    }
}
